package com.client;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.visualization.client.DataTable;

public class PLCharts extends SlidePanel {
	
	private DecoratorPanel dplCharts;
	private DPCharts dpCharts;
	private FlexTable ftFooter;
	private Button btnBack;
	private ListBox lbChType;
	
	private DataTable data = null;

	protected void onFirstShow() {
		dplCharts = new DecoratorPanel();
		dpCharts = new DPCharts();
		dpCharts.setAnimationEnabled(true);
		
		ftFooter = new FlexTable();
		btnBack = new Button("Back to Search", new ClickHandler() {
			public void onClick(ClickEvent event) {
				CNStatsFront.get().plBody.showWidget(0);
			}
		});
		lbChType = new ListBox(false);
		lbChType.addItem("Area", "Area");
		lbChType.addItem("Bar", "Bar");
		lbChType.addItem("Column", "Column");
		lbChType.addItem("Line", "Line");
		lbChType.addItem("Pie", "Pie");
		lbChType.addItem("Table", "Table");
		lbChType.addChangeHandler(new ChangeHandler() {

			public void onChange(ChangeEvent event) {
				String type = lbChType.getValue(lbChType.getSelectedIndex());
				if("Area" == type) dpCharts.drawAreaChart(data);
				else if("Bar" == type) dpCharts.drawBarChart(data);
				else if("Column" == type) dpCharts.drawColumnChart(data);
				else if("Line" == type) dpCharts.drawLineChart(data);
				else if("Pie" == type) dpCharts.drawPieChart(data);
				else if("Table" == type) dpCharts.drawTableChart(data);
			}
			
		});

		this.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		
		dplCharts.add(dpCharts);
		this.add(dplCharts);
		this.setCellVerticalAlignment(dplCharts, HasVerticalAlignment.ALIGN_MIDDLE);
		
		this.add(ftFooter);
		ftFooter.setHTML(0, 0, "Chart Type:");
		ftFooter.setWidget(0, 1, lbChType);		
		this.setCellVerticalAlignment(ftFooter, HasVerticalAlignment.ALIGN_TOP);
		
		this.add(btnBack);
	}
	
	public void setData(DataTable data) {
		this.data = data;
	}
	
	public void draw() {
		dpCharts.drawColumnChart(data);
		lbChType.setSelectedIndex(2);
	}
	
}
